package com.lesent.hadoop.mapreducer.multi;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @author shengtao
 * @Description: reader
 * @date 2018/12/29 13:34
 */
public class MyRecordReader extends RecordReader<NullWritable,BytesWritable>{

    private FileSplit fileSplit;

    private Configuration configuration;

    private BytesWritable bytesWritable = new BytesWritable();

    private boolean processed = false;

    @Override
    public void initialize(InputSplit inputSplit, TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        this.fileSplit = (FileSplit) inputSplit;
        this.configuration = taskAttemptContext.getConfiguration();
    }

    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {
        if(!processed){
            byte[] content = new byte[(int) fileSplit.getLength()];
            Path file = fileSplit.getPath();
            FileSystem fileSystem = file.getFileSystem(configuration);
            FSDataInputStream in = null;
            try {
                in = fileSystem.open(file);
                IOUtils.readFully(in,content,0,content.length - 1);
                bytesWritable.set(content,0,content.length - 1);
                processed = true;
                return true;
            }finally {
                IOUtils.closeStream(in);
            }
        }
        return false;
    }

    @Override
    public NullWritable getCurrentKey() throws IOException, InterruptedException {
        return NullWritable.get();
    }

    @Override
    public BytesWritable getCurrentValue() throws IOException, InterruptedException {
        return bytesWritable;
    }

    @Override
    public float getProgress() throws IOException, InterruptedException {
        return processed ? 1.0F : 0.0F;
    }

    @Override
    public void close() throws IOException {

    }
}
